﻿/*
 *  Developer   :Tony Hicks
 *  Description :
 *  History     :
 */

using QuickApp.DbInterface;
using QuickApp.DbInterface.Helpers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;

namespace QuickApp.SqlSilver
{
    public class SqlServerManager : QADbManager
    {
        #region Protected Methods

        protected override object getCommand(string command, DbCommandType cmd)
        {
            using(var conn = connection.GetNewConnection() as SqlConnection)
            {
                conn.Open();
                SqlCommand exe = conn.CreateCommand();
                exe.CommandText = command;
                switch (cmd)
                {
                    case DbCommandType.SelectDataTable:
                        DataTable dt = new DataTable();
                        dt.Load(exe.ExecuteReader());
                        return dt;

                    case DbCommandType.SelectScalar:
                        return exe.ExecuteScalar();

                    case DbCommandType.UpdateInsert:
                        return exe.ExecuteNonQuery();
                }
                return null;
            }
            
        }

        protected override void loadDatabases()
        {
            databases = new List<QADbDatabase>();
            foreach (DataRow dr in this.SelectTable("EXEC sp_databases ").Rows)
                databases.Add(new SqlServerDatabase(dr[0].ToString().EncloseInSquareBrackets(), dr[0].ToString(), this));
        }

        #endregion Protected Methods

        //public override bool SaveChanges(DataTable dtUpdate, string dbName, string tableName)
        //{
        //    foreach (DataRow dr in dtUpdate.GetChanges().Rows)
        //    {
        //        switch(dr.RowState)
        //        {
        //            case DataRowState.Added:
        //                break;
        //            case DataRowState.Deleted:
        //                break;
        //            case DataRowState.Modified:
        //                break;
        //        }
        //    }
        //}

        public QADbConnection Connection
        {
            get { return connection; }
        }

        #region Public Constructors

        public SqlServerManager()
        {
        }

        #endregion Public Constructors

        #region Public Methods

        public override bool CanConnect()
        {

            using (SqlConnection conn = connection.GetNewConnection() as SqlConnection)
            {
                try
                {
                    conn.Open();
                    return true;
                }
                catch (SqlException)
                {
                    return false;
                }
            }
        }

        public override bool SetConnection(QADbConnection connection)
        {
            this.connection = connection;
            return CanConnect();
        }
        #endregion Public Methods
    }
}